const access_key = '987654'
const base_url = import.meta.env.VITE_BASE_URL
const timeout = 5000

const request = (config) => {
	const url = config.url
	const method = config.method || 'GET'
	const data = config.data || {}
	const header = {
		'access-key': access_key,
		...config.header
	}
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method,
			header,
			data,
			timeout,
			success: (response) => {
				// console.log('response', response)
				if (response.data.errCode === 0) {
					resolve(response.data)
				}
				else if (response.data.errCode === 400) {
					console.log('error', response.data)
					uni.showToast({
						title: response.data.errMsg,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail: (error) => {
				console.log('error', error)
			}
		})
	})
}

export default request
